package com.hyh.music.service.Impl;

import com.hyh.music.Expections.AccountNotFoundException;
import com.hyh.music.Expections.PasswordErrorException;
import com.hyh.music.constant.MessageConstant;
import com.hyh.music.domain.domainDTO.AdminLoginDTO;
import com.hyh.music.domain.domains.Admin;
import com.hyh.music.mapper.AdminMapper;
import com.hyh.music.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    /**
     * 用户登录校验
     *
     * @param adminLoginDTO
     * @return
     */
    @Override
    public Admin login(AdminLoginDTO adminLoginDTO) {
        String name = adminLoginDTO.getName();
        String password = adminLoginDTO.getPassword();
        //查数据库看是否存在
        Admin admin = adminMapper.getByName(name);
        //账号不存在的话
        if (admin == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if(!password.equals(admin.getPassword())){
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return admin;
    }
}
